# PLOTS RESPECTABILITY POLITICS
# ALAN YAN
# SEPTEMBER 22, 2020

#### SETUP ####
#clear environment
rm(list = ls())

#load libraries
library(pacman)
p_load(tidyverse,
       dotwhisker,
       broom,
       DeclareDesign,
       scales,
       gridExtra,
       grid,
       stargazer)

#load data
dt <- read_rds("01-data/clean-data")

#### RPS COMPOSITE PLOTS ####
#### *HISTOGRAM OF RPS ####
dt %>%
  filter(
    (gc == 1 | gc == 3) &
      attn_check == 1
  ) %>% 
  ggplot(., aes(rps_index)) +
  geom_histogram(color = "white", fill = "black", aes(y = ..count../sum(..count..)), bins = 21) +
  scale_x_continuous(breaks = seq(0, 1, .2)) +
  scale_y_continuous(label = percent_format(), 
                     breaks = seq(0, .12, .05)) +
  theme_minimal() +
  xlab("Respectability Politics Scale (Composite)") +
  ylab("Percent") -> hist.rps_index
hist.rps_index
ggsave("03-plots/fig-1.pdf", hist.rps_index, width = 6, height = 6)

#### *HISTOGRAM OF RPS BY GENDER ####
dt %>%
  filter(
    (gc == 1 | gc == 3) &
      attn_check == 1
  ) %>% 
  ggplot(., aes(x = rps_index, fill = as.factor(r_sex), color = as.factor(r_sex))) +
  geom_histogram(aes(y = ..density..*5), bins = 21,
                 alpha = 0.3, position = 'identity') +
  scale_y_continuous(labels = function(x) paste0(x, "%")) +
  scale_x_continuous(breaks = seq(0, 1, .2)) +
  theme_minimal() +
  xlab("Respectability Politics Scale (Composite)") +
  ylab("Percent") +
  guides(fill = guide_legend(title="Gender"),
         color = guide_legend(title = "Gender")) -> hist.rps_index.gender
hist.rps_index.gender
ggsave("03-plots/appendix-fig-d3.pdf", hist.rps_index.gender, width = 6, height = 6)

#### *HISTOGRAM OF RPS BY AGE ####
dt %>%
  filter(
    (gc == 1 | gc == 3) &
      attn_check == 1 &
      r_age != "Under 18"
  ) %>% 
  ggplot(., aes(x = r_age, y = rps_index)) +
  geom_boxplot() +
  theme_minimal() +
  ylab("Respectability Politics Scale \n (Composite)") +
  xlab("Age") +
  theme(axis.text.x = element_text(angle = 70,
                                   vjust = .5)) -> hist.rps_index.age
hist.rps_index.age
ggsave("03-plots/appendix-fig-d1.pdf", hist.rps_index.age, width = 6, height = 6)

#### *HISTOGRAM OF RPS BY EDUCATION ####
dt %>%
  filter(
    (gc == 1 | gc == 3) &
      attn_check == 1 
  ) %>% 
  ggplot(., aes(x = r_edu, y = rps_index)) +
  geom_boxplot() +
  theme_minimal() +
  ylab("Respectability Politics Scale \n (Composite)") +
  xlab("Education") +
  theme(axis.text.x = element_text(angle = 70,
                                   vjust = .5)) -> hist.rps_index.edu
hist.rps_index.edu
ggsave("03-plots/appendix-fig-d2.pdf", hist.rps_index.edu, width = 6, height = 6)

#### *HISTOGRAM OF RPS BY PID ####
dt %>%
  filter(
    (gc == 1 | gc == 3) &
      attn_check == 1 
  ) %>% 
  ggplot(., aes(x = r_pid, y = rps_index)) +
  geom_boxplot() +
  theme_minimal() +
  ylab("Respectability Politics Scale \n (Composite)") +
  xlab("Party ID") +
  theme(axis.text.x = element_text(angle = 70,
                                   vjust = .5)) -> hist.rps_index.pid
hist.rps_index.pid
ggsave("03-plots/appendix-fig-d4.pdf", hist.rps_index.pid, width = 6, height = 6)

#### RPS ATTRIBUTION PLOTS####
#### *HISTOGRAM OF RPS ATTRIBUTION ####
dt %>%
  filter(
    (gc == 1 | gc == 3) &
      attn_check == 1
  ) %>%
  ggplot(., aes(rps_attribution)) +
  geom_histogram(color = "white", fill = "black", aes(y = ..count../sum(..count..)), bins = 10) +
  scale_x_continuous(breaks = seq(0, 1, .2)) +
  scale_y_continuous(label = percent_format(),
                     breaks = seq(0, .25, .05)) +
  theme_minimal() +
  xlab("Respectability Politics Scale (Attributional)") +
  ylab("Percent") -> hist.rps_attribution
hist.rps_attribution
ggsave("03-plots/appendix-fig-c1.pdf", hist.rps_attribution, width = 6, height = 6)

#### RPS CONCERN PLOTS####
#### *HISTOGRAM OF RPS CONCERN ####
dt %>%
  filter(
    (gc == 1 | gc == 3) &
      attn_check == 1
  ) %>%
  ggplot(., aes(rps_concern)) +
  geom_histogram(color = "white", fill = "black", aes(y = ..count../sum(..count..)), bins = 10) +
  scale_x_continuous(breaks = seq(0, 1, .2)) +
  scale_y_continuous(label = percent_format(),
                     breaks = seq(0, .25, .05)) +
  theme_minimal() +
  xlab("Respectability Politics Scale (Concern)") +
  ylab("Percent") -> hist.rps_concern
hist.rps_concern
ggsave("03-plots/appendix-fig-c3.pdf", hist.rps_concern, width = 6, height = 6)

#### COEF PLOTS ####
#### *RPS MAIN COEF PLOT ####
dt %>%
  filter(
    (gc == 1 | gc == 3) &
      attn_check == 1 
  ) %>%
lm_robust(rps_index ~ r_age_n + r_edu_n + r_income_n + r_sex + r_south + r_pid_n + r_ideology_n + auth_index +
                         r_religious_imp_n + shame + anger + idimpt_n + closeness_diff + stereotype + linked_fate_n +
                         discblacks_n,
                       data = .) %>%
  tidy() -> rps.model
dwplot(rps.model) %>%
  relabel_predictors(
    "r_age_n" = "Age",
    "r_edu_n" = "Education",
    "r_income_n" = "Income",
    "r_sexFemale" = "Female",
    "r_southSouth" = "South",
    "r_pid_n" = "Party ID (Democrat)",
    "r_ideology_n" = "Ideology (Liberal)",
    "auth_index" = "Authoritarianism",
    "r_religious_imp_n" = "Import. of Religion to Life",
    "shame" = "In-group Shame",
    "anger" = "In-group Anger",
    "idimpt_n" = "Impt. of Black Id. to Self",
    "closeness_diff" = "Relative Closesness to Blacks",
    "stereotype" = "Endorsement of Neg. Stereotypes",
    "linked_fate_n" = "Linked Fate",
    "discblacks_n" = "Perceptions of Discrimination"
  ) +
  geom_vline(xintercept = 0, linetype = "dashed") +
  xlab("Coefficients") +
  scale_x_continuous(breaks = seq(-.5, .4, .05)) +
  xlim(-.5, .4) -> coef_plot.rps
coef_plot.rps
ggsave("03-plots/fig-2.pdf", coef_plot.rps, width = 6, height = 6)

dt %>%
  filter(
    (gc == 1 | gc == 3) &
      attn_check == 1 
  ) %>%
  lm(rps_index ~ r_age_n + r_edu_n + r_income_n + r_sex + r_south + r_pid_n + r_ideology_n + auth_index +
              r_religious_imp_n + shame + anger + idimpt_n + closeness_diff + stereotype + linked_fate_n +
              discblacks_n,
            data = .) %>%
  stargazer(.,
            se = starprep(., se_type = "HC2"),
            covariate.labels = c("Age",
                                 "Education",
                                 "Income",
                                 "Female",
                                 "South",
                                 "Party ID (Democrat)",
                                 "Ideology (Liberal)",
                                 "Authoritarianism",
                                 "Import. of Religion to Life",
                                 "In-group Shame",
                                 "In-group Anger",
                                 "Impt. of Black Id. to Self",
                                 "Relative Closesness to Blacks",
                                 "Endorsement of Neg. Stereotypes",
                                 "Linked Fate",
                                 "Perceptions of Discrimination"
                                 ),
            dep.var.labels = "RPS index",
            style = "apsr",
            omit.stat = "f",
            omit = c("Constant"))

#### *RPS ATTRIBUTION COEF PLOT ####
dt %>%
  filter(
    (gc == 1 | gc == 3) &
      attn_check == 1 
  ) %>%
  lm_robust(rps_attribution ~ r_age_n + r_edu_n + r_income_n + r_sex + r_south + r_pid_n + r_ideology_n + auth_index +
              r_religious_imp_n + shame + anger + idimpt_n + closeness_diff + stereotype + linked_fate_n +
              discblacks_n,
            data = .) %>%
  tidy() -> rps_attribution.model
dwplot(rps_attribution.model) %>%
  relabel_predictors(
    "r_age_n" = "Age",
    "r_edu_n" = "Education",
    "r_income_n" = "Income",
    "r_sexFemale" = "Female",
    "r_southSouth" = "South",
    "r_pid_n" = "Party ID (Democrat)",
    "r_ideology_n" = "Ideology (Liberal)",
    "auth_index" = "Authoritarianism",
    "r_religious_imp_n" = "Import. of Religion to Life",
    "shame" = "In-group Shame",
    "anger" = "In-group Anger",
    "idimpt_n" = "Impt. of Black Id. to Self",
    "closeness_diff" = "Relative Closesness to Blacks",
    "stereotype" = "Endorsement of Neg. Stereotypes",
    "linked_fate_n" = "Linked Fate",
    "discblacks_n" = "Perceptions of Discrimination"
  ) +
  geom_vline(xintercept = 0, linetype = "dashed") +
  xlab("Coefficients") +
  scale_x_continuous(breaks = seq(-.5, .4, .05)) +
  xlim(-.5, .4) -> coef_plot.rps_attribution
coef_plot.rps_attribution
ggsave("03-plots/appendix-fig-c2.pdf", coef_plot.rps_attribution, width = 6, height = 6)



#### *RPS CONCERN COEF PLOT ####
dt %>%
  filter(
    (gc == 1 | gc == 3) &
      attn_check == 1 
  ) %>%
  lm_robust(rps_concern ~ r_age_n + r_edu_n + r_income_n + r_sex + r_south + r_pid_n + r_ideology_n + auth_index +
              r_religious_imp_n + shame + anger + idimpt_n + closeness_diff + stereotype + linked_fate_n +
              discblacks_n,
            data = .) -> rps_concern.model
dwplot(rps_concern.model) %>%
  relabel_predictors(
    "r_age_n" = "Age",
    "r_edu_n" = "Education",
    "r_income_n" = "Income",
    "r_sexFemale" = "Female",
    "r_southSouth" = "South",
    "r_pid_n" = "Party ID (Democrat)",
    "r_ideology_n" = "Ideology (Liberal)",
    "auth_index" = "Authoritarianism",
    "r_religious_imp_n" = "Import. of Religion to Life",
    "shame" = "In-group Shame",
    "anger" = "In-group Anger",
    "idimpt_n" = "Impt. of Black Id. to Self",
    "closeness_diff" = "Relative Closesness to Blacks",
    "stereotype" = "Endorsement of Neg. Stereotypes",
    "linked_fate_n" = "Linked Fate",
    "discblacks_n" = "Perceptions of Discrimination"
  ) +
  geom_vline(xintercept = 0, linetype = "dashed") +
  xlab("Coefficients") +
  scale_x_continuous(breaks = seq(-.5, .4, .05)) +
  xlim(-.5, .4) -> coef_plot.rps_concern
coef_plot.rps_concern
ggsave("03-plots/appendix-fig-c4.pdf", coef_plot.rps_concern, width = 6, height = 6)

#### *PUNITIVE ATTITUDES AND RPS ####
#### **SAGGING PANTS ####
dt %>%
  filter(
    (gc == 1 | gc == 3) &
      attn_check == 1 
  ) %>%
  lm_robust(saggingpants_n ~ r_ideology_n + r_age_n + r_income_n + r_edu_n + r_sex + r_south +
              rps_index + auth_index + r_pid_n + linked_fate_n,
            data = .) %>%
  tidy() %>%
  filter(term %in% c("rps_index",
                     "auth_index",
                     "r_pid_n",
                     "linked_fate_n")) -> model.sagging

dwplot(model.sagging) %>%
  relabel_predictors(
    "rps_index" = "RPS",
    "auth_index" = "Authoritarianism",
    "r_pid_n" = "Party ID (Democrat)",
    "linked_fate_n" = "Linked Fate",
  ) +
  geom_vline(xintercept = 0, linetype = "dashed") +
  ggtitle("Sagging Pants Ordinance") +
  theme(plot.title = element_text(hjust = .5)) +
  scale_x_continuous(breaks = seq(-.4, .6, .2)) +
  xlim(-.4, .6) -> coef_plot.sagging
coef_plot.sagging

#### **HOUSTON DRESS CODE  ####
dt %>%
  filter(
    (gc == 1 | gc == 3) &
      attn_check == 1 
  ) %>%
  lm_robust(houstonschool_n ~ r_ideology_n + r_age_n + r_income_n + r_edu_n + r_sex + r_south +
              rps_index + auth_index + r_pid_n + linked_fate_n,
            data = .) %>%
  tidy() %>%
  filter(term %in% c("rps_index",
                     "auth_index",
                     "r_pid_n",
                     "linked_fate_n")) -> model.houston

dwplot(model.houston) %>%
  relabel_predictors(
    "rps_index" = "RPS",
    "auth_index" = "Authoritarianism",
    "r_pid_n" = "Party ID (Democrat)",
    "linked_fate_n" = "Linked Fate",
  ) +
  geom_vline(xintercept = 0, linetype = "dashed") +
  ggtitle("Houston Dress Code") +
  theme(plot.title = element_text(hjust = .5),
        axis.text.y = element_blank()) +
  scale_x_continuous(breaks = seq(-.4, .6, .2)) +
  xlim(-.4, .6) -> coef_plot.houston
coef_plot.houston

#### **BLM ####
dt %>%
  filter(
    (gc == 1 | gc == 3) &
      attn_check == 1 
  ) %>%
  lm_robust(blm_n ~ r_ideology_n + r_age_n + r_income_n + r_edu_n + r_sex + r_south +
              rps_index + auth_index + r_pid_n + linked_fate_n,
            data = .) %>%
  tidy() %>%
  filter(term %in% c("rps_index",
                     "auth_index",
                     "r_pid_n",
                     "linked_fate_n")) -> model.blm

dwplot(model.blm) %>%
  relabel_predictors(
    "rps_index" = "RPS",
    "auth_index" = "Authoritarianism",
    "r_pid_n" = "Party ID (Democrat)",
    "linked_fate_n" = "Linked Fate",
  ) +
  geom_vline(xintercept = 0, linetype = "dashed") +
  ggtitle("Opposition to Black Lives Matter") +
  theme(plot.title = element_text(hjust = .5)) +
  scale_x_continuous(breaks = seq(-.4, .6, .2)) +
  xlim(-.4, .6) -> coef_plot.blm
coef_plot.blm

#### **THREE STRIKES LAW ####
dt %>%
  filter(
    (gc == 1 | gc == 3) &
      attn_check == 1 
  ) %>%
  lm_robust(threestrikes_n ~ r_ideology_n + r_age_n + r_income_n + r_edu_n + r_sex + r_south +
              rps_index + auth_index + r_pid_n + linked_fate_n,
            data = .) %>%
  tidy() %>%
  filter(term %in% c("rps_index",
                     "auth_index",
                     "r_pid_n",
                     "linked_fate_n")) -> model.threestrikes

dwplot(model.threestrikes) %>%
  relabel_predictors(
    "rps_index" = "RPS",
    "auth_index" = "Authoritarianism",
    "r_pid_n" = "Party ID (Democrat)",
    "linked_fate_n" = "Linked Fate",
  ) +
  geom_vline(xintercept = 0, linetype = "dashed") +
  ggtitle("Three Strikes Law") +
  theme(plot.title = element_text(hjust = .5),
        axis.text.y = element_blank()) +
  scale_x_continuous(breaks = seq(-.4, .6, .2)) +
  xlim(-.4, .6) -> coef_plot.threestrikes
coef_plot.threestrikes

#### **DRUG LAWS ####
dt %>%
  filter(
    (gc == 1 | gc == 3) &
      attn_check == 1 
  ) %>%
  lm_robust(drugcrimes_n ~ r_ideology_n + r_age_n + r_income_n + r_edu_n + r_sex + r_south +
              rps_index + auth_index + r_pid_n + linked_fate_n,
            data = .) %>%
  tidy() %>%
  filter(term %in% c("rps_index",
                     "auth_index",
                     "r_pid_n",
                     "linked_fate_n")) -> model.drugs

dwplot(model.drugs) %>%
  relabel_predictors(
    "rps_index" = "RPS",
    "auth_index" = "Authoritarianism",
    "r_pid_n" = "Party ID (Democrat)",
    "linked_fate_n" = "Linked Fate",
  ) +
  geom_vline(xintercept = 0, linetype = "dashed") +
  ggtitle("Harsher Drug Laws") +
  theme(plot.title = element_text(hjust = .5)) +
  scale_x_continuous(breaks = seq(-.4, .6, .2)) +
  xlim(-.4, .6) -> coef_plot.drugs
coef_plot.drugs

#### **DEATH PENALTY ####
dt %>%
  filter(
    (gc == 1 | gc == 3) &
      attn_check == 1 
  ) %>%
  lm_robust(deathpenalty_n ~ r_ideology_n + r_age_n + r_income_n + r_edu_n + r_sex + r_south +
              rps_index + auth_index + r_pid_n + linked_fate_n,
            data = .) %>%
  tidy() %>%
  filter(term %in% c("rps_index",
                     "auth_index",
                     "r_pid_n",
                     "linked_fate_n")) -> model.deathpenalty

dwplot(model.deathpenalty) %>%
  relabel_predictors(
    "rps_index" = "RPS",
    "auth_index" = "Authoritarianism",
    "r_pid_n" = "Party ID (Democrat)",
    "linked_fate_n" = "Linked Fate",
  ) +
  geom_vline(xintercept = 0, linetype = "dashed") +
  ggtitle("Death Penalty") +
  theme(plot.title = element_text(hjust = .5),
        axis.text.y = element_blank()) +
  scale_x_continuous(breaks = seq(-.4, .6, .2)) +
  xlim(-.4, .6) -> coef_plot.deathpenalty
coef_plot.deathpenalty

#### **POLICE SHOOTING ####
dt %>%
  filter(
    (gc == 1 | gc == 3) &
      attn_check == 1 
  ) %>%
  lm_robust(justifiedshooting_n ~ r_ideology_n + r_age_n + r_income_n + r_edu_n + r_sex + r_south +
              rps_index + auth_index + r_pid_n + linked_fate_n,
            data = .) %>%
  tidy() %>%
  filter(term %in% c("rps_index",
                     "auth_index",
                     "r_pid_n",
                     "linked_fate_n")) -> model.shooting

dwplot(model.shooting) %>%
  relabel_predictors(
    "rps_index" = "RPS",
    "auth_index" = "Authoritarianism",
    "r_pid_n" = "Party ID (Democrat)",
    "linked_fate_n" = "Linked Fate",
  ) +
  geom_vline(xintercept = 0, linetype = "dashed") +
  ggtitle("How Often Police Shootings Justified") +
  theme(plot.title = element_text(hjust = .5)) +
  scale_x_continuous(breaks = seq(-.4, .6, .2)) +
  xlim(-.4, .6) -> coef_plot.shooting
coef_plot.shooting

#### ** WELFARE SPENDING####
dt %>%
  filter(
    (gc == 1 | gc == 3) &
      attn_check == 1 
  ) %>%
  lm_robust(welfarespending_n ~ r_ideology_n + r_age_n + r_income_n + r_edu_n + r_sex + r_south +
              rps_index + auth_index + r_pid_n + linked_fate_n,
            data = .) %>%
  tidy() %>%
  filter(term %in% c("rps_index",
                     "auth_index",
                     "r_pid_n",
                     "linked_fate_n")) -> model.welfarespending

dwplot(model.welfarespending) %>%
  relabel_predictors(
    "rps_index" = "RPS",
    "auth_index" = "Authoritarianism",
    "r_pid_n" = "Party ID (Democrat)",
    "linked_fate_n" = "Linked Fate",
  ) +
  geom_vline(xintercept = 0, linetype = "dashed") +
  ggtitle("Decrease Welfare Spending") +
  theme(plot.title = element_text(hjust = .5),
        axis.text.y = element_blank()) +
  scale_x_continuous(breaks = seq(-.4, .6, .2)) +
  xlim(-.4, .6) -> coef_plot.welfarespending
coef_plot.welfarespending

#### ** WELFARE WORK ####
dt %>%
  filter(
    (gc == 1 | gc == 3) &
      attn_check == 1 
  ) %>%
  lm_robust(welfarework_n ~ r_ideology_n + r_age_n + r_income_n + r_edu_n + r_sex + r_south +
              rps_index + auth_index + r_pid_n + linked_fate_n,
            data = .) %>%
  tidy() %>%
  filter(term %in% c("rps_index",
                     "auth_index",
                     "r_pid_n",
                     "linked_fate_n")) -> model.welfarework

dwplot(model.welfarework) %>%
  relabel_predictors(
    "rps_index" = "RPS",
    "auth_index" = "Authoritarianism",
    "r_pid_n" = "Party ID (Democrat)",
    "linked_fate_n" = "Linked Fate",
  ) +
  geom_vline(xintercept = 0, linetype = "dashed") +
  ggtitle("Importance of Welfare Work Requirement") +
  theme(plot.title = element_text(hjust = .5)) +
  scale_x_continuous(breaks = seq(-.4, .6, .2)) +
  xlim(-.4, .6) -> coef_plot.welfarework
coef_plot.welfarework

#### ** TRUANCY ####
dt %>%
  filter(
    (gc == 1 | gc == 3) &
      attn_check == 1 
  ) %>%
  lm_robust(truancy_n ~ r_ideology_n + r_age_n + r_income_n + r_edu_n + r_sex + r_south +
              rps_index + auth_index + r_pid_n + linked_fate_n,
            data = .) %>%
  tidy() %>%
  filter(term %in% c("rps_index",
                     "auth_index",
                     "r_pid_n",
                     "linked_fate_n")) -> model.truancy

dwplot(model.truancy) %>%
  relabel_predictors(
    "rps_index" = "RPS",
    "auth_index" = "Authoritarianism",
    "r_pid_n" = "Party ID (Democrat)",
    "linked_fate_n" = "Linked Fate",
  ) +
  geom_vline(xintercept = 0, linetype = "dashed") +
  ggtitle("Jail Parents of Truants") +
  theme(plot.title = element_text(hjust = .5),
        axis.text.y = element_blank()) +
  scale_x_continuous(breaks = seq(-.4, .6, .2)) +
  xlim(-.4, .6) -> coef_plot.truancy
coef_plot.truancy

#### *JOINING PUNITIVE POLICY PLOTS ####
grid.arrange(coef_plot.sagging,
             coef_plot.houston,
             coef_plot.blm,
             coef_plot.threestrikes,
             coef_plot.drugs,
             coef_plot.deathpenalty,
             coef_plot.shooting,
             coef_plot.welfarespending,
             coef_plot.welfarework,
             coef_plot.truancy,
             ncol = 2,
             nrow = 5,
             widths = c(1.3, 1),
             heights = c(2, 2, 2, 2, 2),
             top = textGrob("Respectability and Punitive Attitudes among Blacks", gp = gpar(fontsize = 20))) -> coef_plot.punitive
ggsave("03-plots/fig-3.pdf", coef_plot.punitive, width = 10, height = 12)
